import platform, re
from config import *

# 获取当前操作系统
os_type = platform.system()


# 执行dml
def func3(args_dict):
    source_path = args_dict["source"]
    execute_dml(args_dict, source_path)


# 打印
def func2(args_dict):
    output_path = args_dict["output"]
    print_file(config_analysis(args_dict), output_path)


# 导出
def func1(args_dict):
    output_path = args_dict["output"]
    output_file(config_analysis(args_dict), output_path)


# 定义正则表达式
regex_patterns = {
    "source": (
        r"^/.+" if os_type in ["Linux", "Darwin"] else r"^[A-Za-z]:\\.+"
    ),  # Linux和macOS以/开头，Windows以驱动器字母开头
    "output": r"^/.+" if os_type in ["Linux", "Darwin"] else r"^[A-Za-z]:\\.+",
    "dbType": r"^(mysql|oracle)$",  # 数据库类型不变
    "dbInfo": r"^[^:]+:[^@]+@[^:]+:\d+\/[^\/]+$",  # 数据库连接信息不变
    "format": r"^(csv|xlsx|json|sql)$",  # 输出格式不变
}

extra_params = [
    {
        "label": "源配置目录",
        "value": "source",
        "func": lambda args_dict, key, value: (
            args_dict.__setitem__(key, value) or True
            if re.match(regex_patterns["source"], value)
            else print(f"{key} 格式不正确") or False
        ),
    },
    {
        "label": "文件输出目录",
        "value": "output",
        "func": lambda args_dict, key, value: (
            args_dict.__setitem__(key, value) or True
            if re.match(regex_patterns["output"], value)
            else print(f"{key} 格式不正确") or False
        ),
    },
    {
        "label": "数据库类型(mysql|oracle)",
        "value": "dbType",
        "func": lambda args_dict, key, value: (
            args_dict.__setitem__(key, value) or True
            if re.match(regex_patterns["dbType"], value)
            else print(f"{key} 格式不正确，请在 mysql|oracle 选择输入") or False
        ),
    },
    {
        "label": "数据库连接信息",
        "value": "dbInfo",
        "func": lambda args_dict, key, value: (
            args_dict.__setitem__(key, value) or True
            if re.match(regex_patterns["dbInfo"], value)
            else print(
                f"{key} 格式不正确，MySQL 格式为 user:password@host:port/database，"
                "Oracle 格式为 user:password@host:port/service_name"
            )
            or False
        ),
    },
    {
        "label": "输出文件格式(csv|xlsx|json|sql)",
        "value": "format",
        "func": lambda args_dict, key, value: (
            args_dict.__setitem__(key, value) or True
            if re.match(regex_patterns["format"], value)
            else print(f"{key} 格式不正确，请在 csv|xlsx|json|sql 选择输入") or False
        ),
    },
]

mutex_opt = [
    {"label": "导出数据", "value": "export", "func": func1},
    {"label": "打印数据", "value": "print", "func": func2},
    {"label": "执行DML", "value": "dml", "func": func3},
]


__all__ = ["mutex_opt", "extra_params"]
